﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiXuLong.Model;
namespace ShiXuLong.Service
{
    public class UserService
    {
        ShoesEntities db = new ShoesEntities();

        /// <summary>
        /// 获取用户表的数据
        /// </summary>
        /// <returns></returns>
        public List<UserModel> GetShow()
        {
            var user = from item in db.Users.Where(P => true)
                       select new UserModel
                       {
                           Id = item.Id,
                           UserName = item.UserName,
                           UserPwd = item.UserPwd,
                           UserTypeId = item.UserTypeId,
                           UserSex = item.UserSex,
                           UserAge = item.UserAge,
                           Phone = item.Phone,
                           Email = item.Email,
                           Province = item.Province,
                           City = item.City,
                           Area = item.Area,
                           AddRess = item.AddRess,
                           CreateTime = item.CreateTime
                       };
            return user.ToList();
        }
        /// <summary>
        /// 根据id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserModel Edit(int id)
        {
            var user = from item in db.Users.Where(p => p.Id == id)
                       select new UserModel
                       {
                           Id = item.Id,
                           UserName = item.UserName,
                           UserPwd = item.UserPwd,
                           UserTypeId = item.UserTypeId,
                           UserSex = item.UserSex,
                           UserAge = item.UserAge,
                           Phone = item.Phone,
                           Email = item.Email,
                           Province = item.Province,
                           City = item.City,
                           Area = item.Area,
                           AddRess = item.AddRess,
                           CreateTime = item.CreateTime
                       };
            return user.SingleOrDefault();
        }
        public int EditInfo(User users)
        {
            db.Entry(users).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }

        public List<DizhibiaoModel> GetDizhi()
        {
            var user = from item in db.Addresses.Where(P => true)
                       select new DizhibiaoModel
                       {
                           Id = item.Id,
                           //YonghuId = item.YonghuId,
                           //Sheng = item.Sheng,
                           //Shi = item.Shi,
                           //Xian = item.Xian,
                           //Xiang = item.Xiang,
                           //Xiangxi = item.Xiangxi,
                       };
            return user.ToList();
        }
        public DizhibiaoModel Show(int id)
        {
            var user = from item in db.Addresses.Where(p => p.Id == id)
                       select new DizhibiaoModel
                       {
                           Id = item.Id,
                           //YonghuId = item.YonghuId,
                           //Sheng = item.Sheng,
                           //Shi = item.Shi,
                           //Xian = item.Xian,
                           //Xiang = item.Xiang,
                           //Xiangxi = item.Xiangxi,
                       };
            return user.SingleOrDefault();
        }
        public int EditDiZhi(Address dizhi)
        {
            db.Entry(dizhi).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }

        public List<Address> GetUserAddressById(int id)
        {
            var lists = db.Addresses.Where(p => p.UserId == id);

            var userAddressLists = lists.ToList();
            return userAddressLists;
        }
        public int AddUserAddress(Address address) {
            try
            {
                db.Addresses.Add(address);
                var res = db.SaveChanges();
                return res;
            }
            catch (Exception)
            {

                return 0;
            }
           
        }
    }
}
